import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import ElementUI from '../components/ElementUI.vue'

import Admin from '../components/home/Admin.vue'
import Item2 from '../components/admin/Item2.vue'
import Dept from '../components/admin/dept.vue'
import Emp from '../components/admin/emp.vue'

import Qiantai from '../components/home/Qiantai.vue'
import User from '../components/qiantai/user.vue'

import Guke from '../components/home/Guke.vue'
import Finalorders from '../components/guke/finalorders.vue'
import Item from '../components/guke/Item.vue'
import Cart from '../components/guke/cart.vue'
import Sub from '../components/guke/sub.vue'
import Sure from '../components/guke/sure.vue'
import Room from '../components/qiantai/room.vue'


//使用路由机制
Vue.use(VueRouter)
const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login,
  },
  {
    path: '/elementUI',
    component: ElementUI
  },

  {
    path: '/admin',
    component: Admin,
    children: [{
        path: '/dept',
        component: Dept
      },
      {
        path: '/item2',
        component: Item2
      },
      {
        path: '/dept/emp',
        component: Emp
      }
    ]
  },
  {
    path: '/guke',
    component: Guke,
    children: [{
        path: '/orders',
        component: Finalorders
      },
      {
        path: '/item',
        component: Item
      },
      {
        path: '/cart',
        component: Cart
      },
      {
        path: '/sub',
        component: Sub
      },
      {
        path: '/sub/sure',
        component: Sure
      }
    ]
  },
  {
    path: '/qiantai',
    component: Qiantai,
    children: [
      {
      path: '/user',
      component: User
      },
      {
        path: '/room',
        component: Room
      }
    ]
  }
]

//路由导航守卫!!!!!!!

const router = new VueRouter({
  routes
})


router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  //获取token数据信息
  let token = window.sessionStorage.getItem('token')
  if (token === null || token === '') {
    return next('/login')
  }
  //放行请求
  next()
})

export default router
